package com.ruoyi.job.task;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.entity.system.SysConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.baidu.BaiduTtsTokenHolder;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysScenicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Component("wjTask")
@Slf4j
public class WjTask {
    @Autowired
    private ISysScenicService sysScenicService;

    @Value("${yingshiyun.appKey}")
    private String yingshiyunAppKey;

    @Value("${yingshiyun.secret}")
    private String yingshiyunSecret;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private BaiduTtsTokenHolder baiduTtsTokenHolder;

    /**
     * 修改景区的运营状态。
     *
     * @param
     * @return
     * @author ZJW
     * @createTime 2023/7/12 23:33
     */
    public void changeOperationStatus(Integer scenicId, String status) {
        log.info("changeOperationStatus scenicId:{},status:{}", scenicId, status);
        sysScenicService.changeOperationStatus(Long.valueOf(scenicId), status);
    }

    /**
     * 定期获取萤石云AccessToken。
     */
//    public void getYingshiyunAccessToken(String yingshiyunAppKey, String yingshiyunSecret) {
    public void getYingshiyunAccessToken() {
        log.info("getYingshiyunAccessToken yingshiyunAppKey: {},yingshiyunSecret: {}", yingshiyunAppKey, yingshiyunSecret);
        String url = "https://open.ys7.com/api/lapp/token/get";
        Map<String, Object> params = new HashMap<>();
        params.put("appKey", yingshiyunAppKey);
        params.put("appSecret", yingshiyunSecret);
        String responseBody = HttpUtil.post(url, params);
        log.info("responseBody: {}", responseBody);
        if (StringUtils.isNotEmpty(responseBody)) {
            JSONObject resultObject = JSONObject.parseObject(responseBody);
            String code = resultObject.getString("code");
            if (Objects.equals(code, "200")) {
                JSONObject data = resultObject.getJSONObject("data");
                String accessToken = data.getString("accessToken");
                String expireTime = data.getString("expireTime");
                log.info("accessToken: {}, expireTime: {}", accessToken, expireTime);

                SysConfig config = new SysConfig();
                config.setConfigKey("yingshiyun.accessToken");
                SysConfig accessTokenConfig = configService.selectConfigByKey("yingshiyun.accessToken");
                if (Objects.nonNull(accessTokenConfig)) {
                    accessTokenConfig.setConfigValue(accessToken);
                    configService.updateConfig(accessTokenConfig);
                }
            }
        }
    }

    /**
     * 定期更新百度智能云accessToken。
     */
    public void refreshBaiduTtsAccessToken() {
        String token = baiduTtsTokenHolder.refreshBaiduTtsAccessToken();
        if (StringUtils.isNotEmpty(token)) {
            SysConfig config = new SysConfig();
            config.setConfigKey("baiduAi.AccessToken");
            SysConfig accessTokenConfig = configService.selectConfigByKey("baiduAi.AccessToken");
            if (Objects.nonNull(accessTokenConfig)) {
                accessTokenConfig.setConfigValue(token);
                configService.updateConfig(accessTokenConfig);
            }
        }
    }
}
